﻿# -*- coding:utf-8 -*-
# 
#  импорт списка отделов ГИМ
#
from collections import defaultdict



def _print(argStr):
    print unicode(argStr, 'cp1251')


res = defaultdict(list)
dept_id = None


print "Import part unit..." 
Department = get_model('shm', 'Department')

for line in open("shm_department_modify.csv"):
    uline = unicode(line, "CP1251").split(';', 5)

    #str = u'%s %s %s %s %s' % (uline[0],uline[1],uline[2],uline[3],uline[4])
    #print str
    
    name = uline[0]
    code = uline[1]
    id = uline[2]
    parent = uline[3]
    level = uline[4]
    
    if id == '':
        # Add new department
        print u'add    %s' % name
        
        
                
        #skiped = 0
        #affected = 0 
        
        
       

        
    else: 
        # Update name of existing department
        print u'update %s' % name 




    
    #if uline[1] != '':
    #    dept_name = uline[0].replace('\n','')
    #    dept_id = uline[1].replace('\n','')
    #    post = uline[2].replace('\n','')
    #else:
    #    if dept_id:
    #        res[dept_id].append([dept_name, dept_id, uline[2].replace('\n',''), post])    


# обработка ошибки открытия файла
#try:
#    result_file = open("result.txt", 'w')
#except IOError, err:
#    print err.strerror

#res.sort()
      
#for item in res:
#    str = u'%s;%s;\n' % (res[item][0][0],res[item][0][1])
#    result_file.write(str.encode("CP1251"))
        


#result_file.close()
